package com.cxx.purchasecharge.dal.impl;

import javax.persistence.Query;

import org.apache.commons.collections.CollectionUtils;

import com.cxx.purchasecharge.core.model.persistence.Role;
import com.cxx.purchasecharge.dal.RoleDao;
import com.cxx.purchasecharge.dal.common.MyGenericDaoImpl;

public class RoleDaoImpl extends MyGenericDaoImpl<Role, Long> implements RoleDao {

	public RoleDaoImpl() {
		super(Role.class);
	}

	@Override
	public Role findByName(String name) {
		String sql = "select a from Role a where a.name=?1";
		Query query = getEntityManager ().createQuery (sql, Role.class);
		query.setParameter (1, name);
		return CollectionUtils.isNotEmpty (query.getResultList ()) ? (Role)query.getResultList ().get (0) : null;
	}

	@Override
	public boolean isSystem(String name) {
		String sql = "select a.system from Role a where a.name=?1";
		Query query = getEntityManager ().createQuery (sql, Role.class);
        query.setParameter (1, name);
        if(CollectionUtils.isNotEmpty (query.getResultList ()) && null != query.getResultList ().get (0)) 
        {
            return Boolean.parseBoolean (query.getResultList ().get (0).toString ());
        }
		return false;
	}

}
